/**
 * Koa中 koa-bodyparser中间件获取表单提交数据
 * app.use(bodyParser())
 * ctx.request.body 获取表单提交的数据
 */
const Koa = require('koa')
const router = require('koa-router')()
const views = require('koa-views')
const ejs = require('ejs')
const bodyParser = require('koa-bodyparser')


const app = new Koa()

app.use(views('views', {
  extension: 'ejs'
}))

//配置post bodyparser的中间件
app.use(bodyParser())

router.get('/', async (ctx) => {
  await ctx.render('index')
})
// 接收post提交的数据
router.post('/doAdd', async (ctx) => {
  // 原生nodejs在koa中获取表单提交的数据
  const data = ctx.request.body
  console.log(data)
  ctx.body = data
})


//启动路由
app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000)